<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>搭建基于Gitee文档笔记自动发布</title>
  <style type="text/css">
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
  <style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(title);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  </style>
</head>
<body>
<h2 id="搭建基于gitee文档笔记自动发布">搭建基于Gitee文档笔记自动发布</h2>
<blockquote>
<p>由于现在gitee不支持代理静态页面，并且github.io需要VPN，实际使用的话gitee更为方便。</p>
</blockquote>
<h3 id="一为服务器和个人pc添加免密push和pull">一、为服务器和个人PC添加免密push和pull</h3>
<h4 id="参考链接httpshelp.gitee.combaseaccountsshe585ace992a5e8aebee7bdae">参考链接：https://help.gitee.com/base/account/SSH%E5%85%AC%E9%92%A5%E8%AE%BE%E7%BD%AE</h4>
<h3 id="二创建webhooks">二、创建webhooks</h3>
<blockquote>
<p>创建webhooks目的是用于监听gitee仓库的push事件，自动触发服务器端pull更新代码和文档笔记。</p>
</blockquote>
<figure>
<img src="../../../sources/images/image-20250630151019220.png" alt="image-20250630151019220" /><figcaption>image-20250630151019220</figcaption>
</figure>
<h3 id="三创建服务监听">三、创建服务监听</h3>
<h4 id="创建监听脚本文件">1、创建监听脚本文件</h4>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb1-1" title="1"><span class="co">#创建日志存储目录和脚本文件</span></a>
<a class="sourceLine" id="cb1-2" title="2"><span class="fu">sudo</span> mkdir -p /root/gitee-hooks</a>
<a class="sourceLine" id="cb1-3" title="3"><span class="ex">vim</span> gitee-hook.sh</a></code></pre></div>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb2-1" title="1"><span class="co">#!/bin/bash</span></a>
<a class="sourceLine" id="cb2-2" title="2"></a>
<a class="sourceLine" id="cb2-3" title="3"><span class="co">#Git 仓库所在目录，即执行 git pull 的工作目录</span></a>
<a class="sourceLine" id="cb2-4" title="4"><span class="va">WORK_DIR=</span><span class="st">&quot;/root/helloops/&quot;</span></a>
<a class="sourceLine" id="cb2-5" title="5"><span class="co">#日志文件路径，记录脚本执行过程和结果</span></a>
<a class="sourceLine" id="cb2-6" title="6"><span class="va">LOG_FILE=</span><span class="st">&quot;/root/gitee-hooks/hook.log&quot;</span></a>
<a class="sourceLine" id="cb2-7" title="7"></a>
<a class="sourceLine" id="cb2-8" title="8"><span class="co">#将带时间戳的日志信息追加到 LOG_FILE</span></a>
<a class="sourceLine" id="cb2-9" title="9"><span class="co">#示例输出：[20250630-143000] Starting git pull...</span></a>
<a class="sourceLine" id="cb2-10" title="10"><span class="fu">log()</span> <span class="kw">{</span></a>
<a class="sourceLine" id="cb2-11" title="11">  <span class="bu">echo</span> <span class="st">&quot;[</span><span class="va">$(</span><span class="fu">date</span> <span class="st">&#39;+%Y%m%d-%H:%M:%S&#39;</span><span class="va">)</span><span class="st">] </span><span class="va">$1</span><span class="st">&quot;</span> <span class="op">&gt;&gt;</span> <span class="va">$LOG_FILE</span></a>
<a class="sourceLine" id="cb2-12" title="12"><span class="kw">}</span></a>
<a class="sourceLine" id="cb2-13" title="13"></a>
<a class="sourceLine" id="cb2-14" title="14"><span class="co">#切换到工作目录并执行 git pull，将执行过程逐行记录到日志</span></a>
<a class="sourceLine" id="cb2-15" title="15"><span class="fu">execute_git_pull()</span> <span class="kw">{</span></a>
<a class="sourceLine" id="cb2-16" title="16">  <span class="ex">log</span> <span class="st">&quot;start git pull...&quot;</span></a>
<a class="sourceLine" id="cb2-17" title="17">  <span class="bu">cd</span> <span class="va">$WORK_DIR</span> <span class="kw">||</span> <span class="kw">{</span> <span class="ex">log</span> <span class="st">&quot;not cd workdir&quot;</span><span class="kw">;</span> <span class="bu">exit</span> 1<span class="kw">;</span> <span class="kw">}</span></a>
<a class="sourceLine" id="cb2-18" title="18">  <span class="ex">/usr/bin/git</span> pull <span class="op">2&gt;&amp;1</span> <span class="kw">|</span> <span class="kw">while</span> <span class="bu">read</span> <span class="va">line</span>; <span class="kw">do</span></a>
<a class="sourceLine" id="cb2-19" title="19">    <span class="ex">log</span> <span class="st">&quot;git pull: </span><span class="va">$line</span><span class="st">&quot;</span></a>
<a class="sourceLine" id="cb2-20" title="20">  <span class="kw">done</span></a>
<a class="sourceLine" id="cb2-21" title="21">  <span class="ex">log</span> <span class="st">&quot;git pull started...&quot;</span></a>
<a class="sourceLine" id="cb2-22" title="22"><span class="kw">}</span></a>
<a class="sourceLine" id="cb2-23" title="23"></a>
<a class="sourceLine" id="cb2-24" title="24"><span class="co">#解析 Gitee Webhook 发送的 HTTP 请求，验证 Token 并执行更新</span></a>
<a class="sourceLine" id="cb2-25" title="25"><span class="fu">parse_request()</span><span class="kw">{</span></a>
<a class="sourceLine" id="cb2-26" title="26">  <span class="kw">while</span> <span class="bu">read</span> -r <span class="va">line</span>; <span class="kw">do</span></a>
<a class="sourceLine" id="cb2-27" title="27">    <span class="co">#根据自己设置的密码进行更换</span></a>
<a class="sourceLine" id="cb2-28" title="28">    <span class="kw">if [[</span> <span class="va">$line</span> <span class="ot">==</span> <span class="st">&quot;X-Gitee-Token: 123456&quot;</span><span class="kw"> ]]</span>; <span class="kw">then</span> </a>
<a class="sourceLine" id="cb2-29" title="29">        <span class="va">VALID_TOKEN=</span>true</a>
<a class="sourceLine" id="cb2-30" title="30">    <span class="kw">fi</span></a>
<a class="sourceLine" id="cb2-31" title="31">    <span class="kw">if</span><span class="bu"> [</span> <span class="ot">-z</span> <span class="st">&quot;</span><span class="va">$line</span><span class="st">&quot;</span><span class="bu"> ]</span>; <span class="kw">then</span></a>
<a class="sourceLine" id="cb2-32" title="32">       <span class="bu">break</span></a>
<a class="sourceLine" id="cb2-33" title="33">    <span class="kw">fi</span></a>
<a class="sourceLine" id="cb2-34" title="34">  <span class="kw">done</span></a>
<a class="sourceLine" id="cb2-35" title="35"></a>
<a class="sourceLine" id="cb2-36" title="36">  <span class="va">body=$(</span><span class="fu">cat</span><span class="va">)</span></a>
<a class="sourceLine" id="cb2-37" title="37"></a>
<a class="sourceLine" id="cb2-38" title="38">  <span class="ex">log</span> <span class="st">&quot;input gitee hook requests,Body size: </span><span class="va">${#body}</span><span class="st">&quot;</span></a>
<a class="sourceLine" id="cb2-39" title="39"></a>
<a class="sourceLine" id="cb2-40" title="40">  <span class="ex">execute_git_pull</span></a>
<a class="sourceLine" id="cb2-41" title="41"><span class="kw">}</span></a>
<a class="sourceLine" id="cb2-42" title="42"></a>
<a class="sourceLine" id="cb2-43" title="43"><span class="ex">parse_request</span></a></code></pre></div>
<h4 id="创建监听服务文件">2、创建监听服务文件</h4>
<div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb3-1" title="1"><span class="fu">sudo</span> cat <span class="op">&gt;</span> /lib/systemd/system/gitee-hook.service <span class="op">&lt;&lt; &#39;EOF&#39;</span></a>
<a class="sourceLine" id="cb3-2" title="2">[Unit]</a>
<a class="sourceLine" id="cb3-3" title="3">Description=Gitee Webhook Listener</a>
<a class="sourceLine" id="cb3-4" title="4">After=network.target</a>
<a class="sourceLine" id="cb3-5" title="5"></a>
<a class="sourceLine" id="cb3-6" title="6">[Service]</a>
<a class="sourceLine" id="cb3-7" title="7">Type=simple</a>
<a class="sourceLine" id="cb3-8" title="8">ExecStart=/usr/bin/socat TCP-LISTEN:8080,fork EXEC:&quot;/root/gitee-hook.sh&quot;</a>
<a class="sourceLine" id="cb3-9" title="9">WorkingDirectory=/root/gitee-hooks</a>
<a class="sourceLine" id="cb3-10" title="10">User=root</a>
<a class="sourceLine" id="cb3-11" title="11">Group=root</a>
<a class="sourceLine" id="cb3-12" title="12">Restart=on-failure</a>
<a class="sourceLine" id="cb3-13" title="13">RestartSec=5s</a>
<a class="sourceLine" id="cb3-14" title="14">StandardOutput=syslog</a>
<a class="sourceLine" id="cb3-15" title="15">StandardError=syslog</a>
<a class="sourceLine" id="cb3-16" title="16">SyslogIdentifier=gitee-hook</a>
<a class="sourceLine" id="cb3-17" title="17"></a>
<a class="sourceLine" id="cb3-18" title="18">[Install]</a>
<a class="sourceLine" id="cb3-19" title="19">WantedBy=multi-user.target</a>
<a class="sourceLine" id="cb3-20" title="20"><span class="op">EOF</span></a>
<a class="sourceLine" id="cb3-21" title="21"></a>
<a class="sourceLine" id="cb3-22" title="22"><span class="co">#授权脚本</span></a>
<a class="sourceLine" id="cb3-23" title="23"><span class="fu">sudo</span> chmod +x /root/gitee-hook.sh</a>
<a class="sourceLine" id="cb3-24" title="24"></a>
<a class="sourceLine" id="cb3-25" title="25"><span class="co">#检查 socat 是否安装</span></a>
<a class="sourceLine" id="cb3-26" title="26"><span class="fu">which</span> socat <span class="kw">||</span> <span class="fu">sudo</span> apt-get install socat -y  <span class="co"># Debian/Ubuntu</span></a>
<a class="sourceLine" id="cb3-27" title="27"><span class="fu">which</span> socat <span class="kw">||</span> <span class="fu">sudo</span> yum install socat -y       <span class="co"># CentOS/RHEL</span></a>
<a class="sourceLine" id="cb3-28" title="28"></a>
<a class="sourceLine" id="cb3-29" title="29"><span class="co">#重载 systemd 配置并启动</span></a>
<a class="sourceLine" id="cb3-30" title="30"><span class="fu">sudo</span> systemctl daemon-reload</a>
<a class="sourceLine" id="cb3-31" title="31"><span class="fu">sudo</span> systemctl start gitee-hook</a>
<a class="sourceLine" id="cb3-32" title="32"></a>
<a class="sourceLine" id="cb3-33" title="33"><span class="co">#设置开机启动并查看状态</span></a>
<a class="sourceLine" id="cb3-34" title="34"><span class="fu">sudo</span> systemctl enable gitee-hook</a>
<a class="sourceLine" id="cb3-35" title="35"><span class="fu">sudo</span> systemctl status gitee-hook</a></code></pre></div>
<h3 id="四测试操作">四、测试操作</h3>
<h4 id="安装pandoc">1、安装pandoc</h4>
<pre><code>apt -y install pandoc</code></pre>
<h4 id="整个流程操作">2、整个流程操作</h4>
<div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb5-1" title="1"><span class="ex">pandoc</span> sources/md/KL-Ubuntu/搭建基于Gitee文档笔记自动发布.md -s -o html/KL-Ubuntu/搭建基于Gitee文档笔记 自动发布.html</a>
<a class="sourceLine" id="cb5-2" title="2"></a>
<a class="sourceLine" id="cb5-3" title="3"><span class="fu">git</span> add . </a>
<a class="sourceLine" id="cb5-4" title="4"></a>
<a class="sourceLine" id="cb5-5" title="5"><span class="fu">git</span> status </a>
<a class="sourceLine" id="cb5-6" title="6"></a>
<a class="sourceLine" id="cb5-7" title="7"><span class="fu">git</span> commit -m <span class="st">&quot;add **&quot;</span></a>
<a class="sourceLine" id="cb5-8" title="8"></a>
<a class="sourceLine" id="cb5-9" title="9"><span class="fu">git</span> push</a></code></pre></div>
<figure>
<img src="../../../sources/images/image-20250630162754916.png" alt="image-20250630162754916" /><figcaption>image-20250630162754916</figcaption>
</figure>
<h3 id="五nginx配置参考以及目录结构参考">五、Nginx配置参考以及目录结构参考</h3>
<pre><code>server {
  charset utf-8;
  server_name abc.cn www.abc.cn;
  client_max_body_size 1024m;
  gzip on;
  location / {
    alias /root/helloops/html/; 
    autoindex on;
  }
  location /yaml {
    alias /root/helloops/yaml/;
    autoindex on;
  }
  location /windos {
    alias /root/helloops/sources/windos/;
    autoindex on;
    auth_basic &quot;Restricted Access&quot;;
    auth_basic_user_file /etc/nginx/.htpasswd;
    add_header Cache-Control &quot;no-cache, no-store, must-revalidate&quot;;
    add_header Pragma &quot;no-cache&quot;;
    add_header Expires 0;
  }
  location /sources/images {
    alias /root/helloops/sources/images/;
    autoindex on;
  }
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/helloops.cn/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/helloops.cn/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
} 
server {
    if ($host = www.helloops.cn) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = helloops.cn) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen 80;
  server_name abc.cn www.abc.cn;

}</code></pre>
<p><strong>目录结构</strong></p>
<div class="sourceCode" id="cb7"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb7-1" title="1"><span class="ex">root@helloops</span># tree -d</a>
<a class="sourceLine" id="cb7-2" title="2"><span class="ex">.</span></a>
<a class="sourceLine" id="cb7-3" title="3">├── <span class="ex">html</span></a>
<a class="sourceLine" id="cb7-4" title="4">│   ├── <span class="ex">k8s</span></a>
<a class="sourceLine" id="cb7-5" title="5">│   └── <span class="ex">KL-Ubuntu</span></a>
<a class="sourceLine" id="cb7-6" title="6">├── <span class="ex">sources</span></a>
<a class="sourceLine" id="cb7-7" title="7">│   ├── <span class="ex">images</span></a>
<a class="sourceLine" id="cb7-8" title="8">│   ├── <span class="ex">md</span></a>
<a class="sourceLine" id="cb7-9" title="9">│   │   ├── <span class="ex">k8s</span></a>
<a class="sourceLine" id="cb7-10" title="10">│   │   └── <span class="ex">KL-Ubuntu</span></a>
<a class="sourceLine" id="cb7-11" title="11">│   └── <span class="ex">windos</span></a>
<a class="sourceLine" id="cb7-12" title="12">└── <span class="ex">yaml</span></a>
<a class="sourceLine" id="cb7-13" title="13"></a>
<a class="sourceLine" id="cb7-14" title="14"><span class="ex">10</span> directories</a></code></pre></div>
</body>
</html>
